package com.ynny.bookmanagersystem.controller;

import com.ynny.bookmanagersystem.dao.UserDao;
import com.ynny.bookmanagersystem.dao.UserDaoImpl;
import com.ynny.bookmanagersystem.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {

        //登录逻辑
        //1.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("pwd");

        //2.判断用户名和密码是否正确
        UserDao userDao = new UserDaoImpl();

        User user = userDao.selectUserByUserNameAndPassword(username, password);

        if (user != null) {
            //登录成功，跳转到主页

            List<User> users = userDao.selectAllUsers();

            request.getSession().setAttribute("users", users);

            response.sendRedirect("/book/user/list.jsp");


        }


    }
}
